load("@npm//:defs.bzl", "npm_link_all_packages")
load("//packages/zone.js:bundles.bzl", "BUNDLES_ENTRY_POINTS")
load("//packages/zone.js:tools.bzl", "generate_rollup_bundle", "npm_package", "ts_config", "ts_project")

package(default_visibility = ["//visibility:public"])

npm_link_all_packages(
    name = "node_modules",
)

ts_config(
    name = "tsconfig_build",
    src = "tsconfig.json",
)

ts_config(
    name = "tsconfig_test",
    src = "tsconfig-test.json",
    deps = [
        ":tsconfig_build",
        "//packages/zone.js:node_modules/@types/jasmine",
        "//packages/zone.js:node_modules/@types/node",
    ],
)

exports_files([
    "tsconfig.json",
])

genrule(
    name = "LICENSE_copy",
    srcs = ["//:LICENSE"],
    outs = ["LICENSE"],
    cmd = "cp $< $@",
)

genrule(
    name = "LICENSE_wrapped",
    srcs = ["//:LICENSE"],
    outs = ["LICENSE.wrapped"],
    cmd = "(echo '/**\n @license' && cat $< && echo '*/') > $@",
)

ts_project(
    name = "zone_js",
    srcs = ["zone.ts"],
    visibility = ["//visibility:private"],
    deps = ["//packages/zone.js/lib:zone_d_ts"],
)

generate_rollup_bundle(
    bundles = BUNDLES_ENTRY_POINTS,
)

npm_package(
    name = "npm_package",
    srcs = [
        "CHANGELOG.md",
        "README.md",
        "package.json",
        ":LICENSE.wrapped",
        ":LICENSE_copy",
    ] + [
        "//packages/zone.js/bundles:" + b + "-es5.dist"
        for b in BUNDLES_ENTRY_POINTS.keys()
    ] + [
        "//packages/zone.js/bundles:" + b + "-es5.min.dist"
        for b in BUNDLES_ENTRY_POINTS.keys()
    ] + [
        "//packages/zone.js/fesm2015:" + b + "-es2015.dist"
        for b in BUNDLES_ENTRY_POINTS.keys()
    ] + [
        "//packages/zone.js/fesm2015:" + b + "-es2015.min.dist"
        for b in BUNDLES_ENTRY_POINTS.keys()
    ] + [
        ":zone_js_types",
        "//packages/zone.js/lib:zone_d_ts_types",
    ],
    package = "zone.js",
    visibility = ["//visibility:public"],
)

alias(
    name = "pkg",
    actual = ":npm_package",
)
